
package com.bcurtu.service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.bcurtu.db.Account;
import com.bcurtu.db.AccountDao;
import com.bcurtu.james.JamesManager;
import com.bcurtu.james.exceptions.JamesException;

public class TempAliasServiceImpl implements TempAliasService {

	private Logger log=Logger.getLogger(this.getClass());
	
	private AccountDao accountManager;
	private JamesManager james;
	
	public AccountDao getAccountManager() {
		return accountManager;
	}

	public void setAccountManager(AccountDao accountManager) {
		this.accountManager = accountManager;
	}

	public JamesManager getJames() {
		return james;
	}

	public void setJames(JamesManager james) {
		this.james = james;
	}

	public boolean createAccount(String alias, String forward, Integer minutes) {
		Account acc=new Account();
		acc.setAlias(alias);
		acc.setForward(forward);
		Calendar c=Calendar.getInstance();
		c.setTime(new Date());
		c.add(Calendar.MINUTE,minutes);
		acc.setBlockingDate(c.getTime());
		c.add(Calendar.MONTH,1);
		acc.setExpirationDate(c.getTime());
		acc.setActive(Boolean.TRUE);

		log.debug("Inserting Account in DB");
		accountManager.insertAccount(acc);		
		try{
			log.debug("Inserting Account in JAMES");
			james.createAccount(acc.getAlias(),acc.getForward());
			return true;
		} catch (Exception ex){
			log.warn("Error creating account:"+acc.getAlias()+"\n"+ex);
//			throw new UnsupportedOperationException();
			return false;
		}
	}

	public boolean isValidAlias(String alias) {
		return accountManager.isValidAlias(alias);
	}

	public int blockAccounts(Date date) {
		List<Account> accs=accountManager.getBlockingAccounts(date);
		log.debug("Blocking "+accs.size()+" accounts");
		
		for (Account acc:accs){
			try{
				james.deleteAccount(acc.getAlias());
				accountManager.blockAccount(acc.getAlias());
			}catch(JamesException jex){
				try {
					james.verifyFreeAccount(acc.getAlias());
					accountManager.blockAccount(acc.getAlias());					
				} catch (Exception e) {
					log.warn("Can not delete "+acc.getAlias()+" from JAMES:"+e.getMessage());					
				}
			}catch(Exception ex){
				log.warn("Can not delete "+acc.getAlias()+":"+ex.getMessage());
			}
		}
		
		return accs.size();
	}

	public int deleteExpiredAccounts(Date date) {
		return accountManager.deleteExpiredAccounts(new Date());
	}

	public boolean deleteAccountByAlias(String alias) {
		try{
			int i=accountManager.forceDeleteAccount(alias);
			james.deleteAccount(alias);
			return true;
		}catch(Exception ex){
			log.warn("Can not delete account "+alias+":"+ex.getMessage());
			return false;
		}
	}

	public boolean blockAccountByAlias(String alias) {
		int i=accountManager.blockAccount(alias);
		try{
			james.deleteAccount(alias);
		}catch (Exception ex){
			log.warn("Can not delete (block) alias from james: "+alias);
		}
		return i>0;
	}

	public Account getAccountByAliasAndForward(String alias, String forward) {
		return accountManager.getAccountByAliasAndForward(alias,forward);
	}

	public void extendAccount(Account acc, Integer minutes) {
		Calendar c=Calendar.getInstance();
		if (acc.getActive()==Boolean.TRUE){
			c.setTime(acc.getBlockingDate());
		} else {
			c.setTime(new Date());
		}
		c.add(Calendar.MINUTE,minutes);
		acc.setBlockingDate(c.getTime());
		c.add(Calendar.MONTH,1);
		acc.setExpirationDate(c.getTime());
		acc.setActive(Boolean.TRUE);
		
		accountManager.updateAccount(acc);
	}

	
	
}
